﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KantoorDienst
{
    public partial class WeaponEdit : Form
    {
        DBConnect db; 
        public WeaponEdit()
        {
            InitializeComponent();
        }

        private void WeaponEdit_Load(object sender, EventArgs e)
        {
            db = new DBConnect();
            getWeapons();
            getCalibers();
            FillFields();
        }
        private void getCalibers()
        {
            caliberBox.DisplayMember = "name";
            caliberBox.ValueMember = "cal_id";
            caliberBox.DataSource = db.getCaliberList();
            caliberBox.SelectedValue = ((DataRowView)weaponListCmb.SelectedItem)["cal_id"]; //get selected caliber from currently selected weapon
        }
        private void getWeapons()
        {
            weaponListCmb.DisplayMember = "name";
            weaponListCmb.ValueMember = "prod_id";
            weaponListCmb.DataSource = db.getWeaponsList();
        }
        private void FillFields()
        {
            weaponNameTxt.Text = ((DataRowView)weaponListCmb.SelectedItem)["name"].ToString();
            barcodeTxt.Text = ((DataRowView)weaponListCmb.SelectedItem)["barcode"].ToString();
            weaponNumberTxt.Text = ((DataRowView)weaponListCmb.SelectedItem)["item_no"].ToString();
            caliberBox.SelectedValue = ((DataRowView)weaponListCmb.SelectedItem)["cal_id"]; //get selected caliber from currently selected weapon
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            db.setName(weaponNameTxt.Text, weaponListCmb.SelectedValue.ToString()); 
            db.setWeaponnumber(weaponNumberTxt.Text, weaponListCmb.SelectedValue.ToString());
            db.setBarcode(barcodeTxt.Text, weaponListCmb.SelectedValue.ToString());
            db.setCaliber(caliberBox.SelectedValue.ToString(), weaponListCmb.SelectedValue.ToString());
            getWeapons();
        }

        private void Wapennummer_Click(object sender, EventArgs e)
        {

        }

        private void weaponListCmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillFields();
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            db.newWeapon();
            getWeapons();
            weaponListCmb.SelectedIndex = weaponListCmb.Items.Count - 1;
            caliberBox.SelectedIndex = 0;
        }
    }
}
